(loading color.ps...)print


/setcolorspace {
    %[ currentcolor setcolordict currentcolorspace get counttomark 2 add -1 roll
    graphicsdict /currgstate get exch /colorspace exch put
    graphicsdict /currgstate get /colorcomp1 0.0 put
    graphicsdict /currgstate get /colorcomp2 0.0 put
    graphicsdict /currgstate get /colorcomp3 0.0 put
    graphicsdict /currgstate get /colorcomp4 0.0 put
    %exec pop
} bind def

/currentcolorspace {
    graphicsdict /currgstate get /colorspace get
} bind def

/setcolordict <<
    /DeviceGray /setgray cvx
    /DeviceRGB /setrgbcolor cvx
    /DeviceCMYK /setcmykcolor cvx
>> def
/setcolor {
    //setcolordict currentcolorspace get exec
} bind def

/currentcolordict <<
    /DeviceGray /currentgray cvx
    /DeviceRGB /currentrgbcolor cvx
    /DeviceCMYK /currentcmykcolor cvx
>> def
/currentcolor {
    //currentcolordict currentcolorspace get exec
} bind def

/.min { % x y  . x (if x < y)
        %        y (otherwise)
    2 copy gt{exch}if pop
} bind def

/ColorConversion << % <</dest <</source {source-to-dest conversion}>> >>
    /DeviceGray <<
        /DeviceGray {
            graphicsdict /currgstate get /colorcomp1 get
        } bind
        /DeviceRGB {
            graphicsdict /currgstate get /colorcomp1 get
            .3 mul
            graphicsdict /currgstate get /colorcomp2 get
            .59 mul add
            graphicsdict /currgstate get /colorcomp3 get
            .11 mul add
        } bind
        /DeviceCMYK {
            1
            graphicsdict /currgstate get /colorcomp1 get
            .3 mul
            graphicsdict /currgstate get /colorcomp2 get
            .59 mul add
            graphicsdict /currgstate get /colorcomp3 get
            .11 mul add
            graphicsdict /currgstate get /colorcomp4 get
            add
            1 .min sub
        } bind
    >>
    /DeviceRGB <<
        /DeviceGray {
            graphicsdict /currgstate get /colorcomp1 get
            dup
            dup
        } bind
        /DeviceRGB {
            graphicsdict /currgstate get /colorcomp1 get
            graphicsdict /currgstate get /colorcomp2 get
            graphicsdict /currgstate get /colorcomp3 get
        } bind
        /DeviceCMYK {
            graphicsdict /currgstate get /colorcomp4 get % k
            1 graphicsdict /currgstate get /colorcomp1 get % k 1 c
            2 index add 1 .min sub % k 1-min(1,c+k)
            1 graphicsdict /currgstate get /colorcomp2 get % k R 1 m
            3 index add 1 .min sub % k R 1-min(1,m+k)
            1 graphicsdict /currgstate get /colorcomp2 get % k R G 1 y
            5 4 roll add 1 .min sub % R G 1-min(1,y+k)
        } bind
    >>
    /DeviceCMYK <<
        /DeviceGray {
            O
            0
            0
            1 graphicsdict /currgstate get /colorcomp1 get sub
        } bind
        /DeviceRGB {
            1 graphicsdict /currgstate get /colorcomp1 get sub % c
            1 graphicsdict /currgstate get /colorcomp2 get sub % c m
            1 graphicsdict /currgstate get /colorcomp3 get sub % c m y
            3 copy .min .min % c m y k
            %FIXME invoke undercolorremoval and blackgeneration
        } bind
        /DeviceCMYK {
            graphicsdict /currgstate get /colorcomp1 get
            graphicsdict /currgstate get /colorcomp2 get
            graphicsdict /currgstate get /colorcomp3 get
            graphicsdict /currgstate get /colorcomp4 get
        } bind
    >>
>> def

/setgray {
    graphicsdict /currgstate get /colorspace /DeviceGray put
    graphicsdict /currgstate get exch /colorcomp1 exch put
} bind def

/currentgray {
    ColorConversion /DeviceGray get
        graphicsdict /currgstate get /colorspace get
        get exec
} bind def

/setrgbcolor {
    graphicsdict /currgstate get /colorspace /DeviceRGB put
    graphicsdict /currgstate get exch /colorcomp3 exch put
    graphicsdict /currgstate get exch /colorcomp2 exch put
    graphicsdict /currgstate get exch /colorcomp1 exch put
} bind def

/currentrgbcolor {
    ColorConversion /DeviceRGB get
        graphicsdict /currgstate get /colorspace get
        get exec
} bind def

/sethsbcolor {
    graphicsdict /currgstate get /colorspace /DeviceRGB put
    graphicsdict /currgstate get exch /colorcomp3 exch put
    graphicsdict /currgstate get exch /colorcomp2 exch put
    graphicsdict /currgstate get exch /colorcomp1 exch put
} bind def

/currenthsbcolor {
    ColorConversion /DeviceRGB get
        graphicsdict /currgstate get /colorspace get
        get exec
} bind def

/setcmykcolor {
    graphicsdict /currgstate get /colorspace /DeviceCMYK put
    graphicsdict /currgstate get exch /colorcomp4 exch put
    graphicsdict /currgstate get exch /colorcomp3 exch put
    graphicsdict /currgstate get exch /colorcomp2 exch put
    graphicsdict /currgstate get exch /colorcomp1 exch put
} bind def

/currentcmykcolor {
    ColorConversion /DeviceCMYK get
        graphicsdict /currgstate get /colorspace get
        get exec
} bind def

(eof color\n)print
